<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>系统管理</el-breadcrumb-item>
				<el-breadcrumb-item>编辑角色</el-breadcrumb-item>
			</el-breadcrumb>
		</div>

		<div class="content-box">
			<el-tabs v-model="activeName" type="card">
				<el-tab-pane label="基本信息" name="info">
					<div class="tab-content">
						<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
							<el-form-item label="角色类型">
								<el-select v-model="model.roleType" placeholder="请选择...">
									<el-option label="系统管理员" :value="1"></el-option>
									<el-option label="超级管理员" :value="2"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item prop="name" label="角色标识">
								<el-input v-model="model.name" placeholder="唯一名称，不可重复"></el-input>
							</el-form-item>
							<el-form-item prop="title" label="角色名称">
								<el-input v-model="model.title" placeholder="允许中文,128字符内"></el-input>
							</el-form-item>
							<el-form-item label="分配权限" v-if="model.roleType===1">
								<el-card>
									<el-tree :data="model.navigation" node-key="id" default-expand-all :expand-on-click-node="false">
										<span class="tree-node" slot-scope="{ node, data }">
											<span>{{ data.title }}</span>
											<span class="tools-box">
												<el-checkbox size="mini" v-for="item in data.resource" v-model="item.isSelected" :key="item.name">{{item.title}}</el-checkbox>
											</span>
										</span>
									</el-tree>
								</el-card>
							</el-form-item>
						</el-form>
					</div>
				</el-tab-pane>
			</el-tabs>
		</div>
		
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" :loading="loading" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	export default {
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				loading: false,
				model: {
					id: 0,
					name: null,
					title: null,
					roleType: null,
					isSystem: 0,
					navigation: []
				},
				rules: {
					name: [
						{ required: true, message: '请输入角色标识', trigger: 'blur' },
						{ min: 2, max: 128, message: '长度在 2 到 128 个字符', trigger: 'blur' },
						{ pattern: /^[0-9a-zA-Z_]{1,}$/, message: '只能字母数字下划线', trigger: 'blur' }
					],
					title: [
						{ required: true, message: '请输入角色标题', trigger: 'blur' },
					],
					roleType: [
						{ required: true, message: '请选择角色类型', trigger: 'change' }
					]
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this; //当前页面
				if (this.id > 0) {
					_this.$api.request({
						url: '/admin/manager/role/' + this.id,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
					});
				} else {
					_this.$api.request({
						url: '/admin/manager/role/navigation',
						loading: true,
						success(res) {
							//赋值给model菜单
							_this.model.navigation = res.data;
						}
					});
				}
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: '/admin/manager/role/' + _this.model.id,
							data: _this.model,
							successMsg: '修改管理角色已成功',
							beforeSend() {
								_this.loading = true
							},
							success(res) {
								//跳转列表页
								_this.$common.linkUrl('/manager/role/list');
							},
							complete() {
								_this.loading = false
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/manager/role',
							data: _this.model,
							successMsg: '新增管理角色已成功',
							beforeSend() {
								_this.loading = true
							},
							success(res) {
								//重置表单
								_this.$refs['editForm'].resetFields();
							},
							complete() {
								_this.loading = false
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>